import 'package:flutter/material.dart';
import 'package:flutter_changdian_football/base/base_page_controller.dart';
import 'package:flutter_changdian_football/widget/default_page/custom_empty.dart';
import 'package:flutter_changdian_football/widget/default_page/custom_error.dart';
import 'package:flutter_changdian_football/widget/default_page/custom_loading.dart';
import 'package:flutter_changdian_football/widget/screenutil/custom_screenutil.dart';
import 'package:flutter_changdian_football/widget/scroll/no_shadow_scroll_behavior.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';

///
/// 上拉刷新、下拉加载、空白页加载动画
/// 通过在基类BaseGetController中维护上拉刷新、下拉加载、控制器等等状态
/// 不需要暴露给使用者，当然，此方法只能使用 Getx 框架有效
///
class RefreshWidget<Controller extends BaseGetPageController> extends StatefulWidget {
  RefreshWidget({
    Key? key,
    this.enablePullUp = true,
    this.enablePullDown = true,
    this.onRefresh,
    this.onLoadMore,
    required this.child,
    this.empty
  }) : super(key: key);

  final String? tag = null;

  /// 获取 BaseGetController 子类对象，在 GetX 中，任何 BaseGetController 都可以通过此方法获取
  /// 但是必须是没有dispose的Controller
  Controller get getController => GetInstance().find<Controller>(tag: tag);

  /// 是否启用上拉
  bool enablePullUp = true;

  /// 是否启用下拉
  bool enablePullDown = true;

  /// 下拉刷新回调
  VoidCallback? onRefresh;

  /// 上拉加载回调
  VoidCallback? onLoadMore;

  /// 子类，必须是ListView
  Widget child;

  Widget? empty;

  @override
  State<RefreshWidget> createState() => _RefreshWidgetState();
}

// class _RefreshWidgetState extends State<RefreshWidget> with AutomaticKeepAliveClientMixin {
  class _RefreshWidgetState extends State<RefreshWidget> with AutomaticKeepAliveClientMixin {

  /// 内部维护[RefreshController] ，不暴露出去 , 上下刷新控制器
  RefreshController controller = RefreshController(initialRefresh: false);

  @override
  void initState() {
    super.initState();
    widget.getController.initPullLoading(controller);
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    
    return SizedBox(
      width: double.infinity,
      height: double.infinity,
      child: Stack(
        alignment: Alignment.center,
        children: [
          /// 上下刷新、下拉加载控件
          Obx(() => Visibility(
            visible: widget.getController.loadState.value == 1,
            child: ScrollConfiguration(
              behavior: NoShadowScrollBehavior(),
              child: SmartRefresher(
                controller: controller,
                enablePullDown: widget.enablePullDown,
                enablePullUp: widget.enablePullUp,
                onRefresh: () => widget.getController.onLoadRefresh(controller),
                onLoading: () => widget.getController.onLoadMore(controller),
                header: CustomHeader(
                  builder: (BuildContext context, RefreshStatus? mode) {
                    Widget header;
                    if (mode == RefreshStatus.idle) {
                      /// 下拉时显示
                      header = Text(
                        '下拉刷新'.tr,
                        style: TextStyle(fontSize: 14.sp, color: const Color(0xffA7A7A7)),
                      );
                    } else if (mode == RefreshStatus.refreshing) {
                      /// 加载中
                      // header = Lottie.asset("assets/lottie/hg_refresh_head_loading.json", width: 100.w, animate: true);
                      header = Text("刷新中...",
                        style: TextStyle(fontSize: 14.sp, color: const Color(0xffA7A7A7)),);
                    } else if (mode == RefreshStatus.failed) {
                      /// 刷新失败
                      header = Text(
                        '刷新失败'.tr,
                        style: TextStyle(fontSize: 14.sp, color: const Color(0xffA7A7A7)),
                      );
                    } else if (mode == RefreshStatus.completed) {
                      /// 刷新成功
                      header = Text(
                        '刷新成功'.tr,
                        style: TextStyle(fontSize: 14.sp, color: const Color(0xffA7A7A7)),
                      );
                    } else {
                      /// 超过二层
                      header = Text(
                        '释放刷新'.tr,
                        style: TextStyle(fontSize: 14.sp, color: const Color(0xffA7A7A7)),
                      );
                    }
                    return SizedBox(
                      height: 64.w,
                      child: Center(child: header),
                    );
                  },
                ),
                footer: CustomFooter(
                  builder: (BuildContext context, LoadStatus? mode) {
                    Widget footer;
                    if (mode == LoadStatus.idle) {
                      /// 下拉提示
                      footer = Text("上拉加载".tr,
                        style: TextStyle(fontSize: 14.sp, color: const Color(0xffA7A7A7)),);
                    } else if (mode == LoadStatus.loading) {
                      /// 加载中
                      // footer = Lottie.asset("assets/lottie/hg_refresh_footer.json", width: 200.w, animate: true);

                      footer = Text("加载中...",
                        style: TextStyle(fontSize: 14.sp, color: const Color(0xffA7A7A7)),);
                    } else if (mode == LoadStatus.failed) {
                      /// 加载失败
                      footer = Text(
                        '加载失败'.tr,
                        style: TextStyle(fontSize: 14.sp, color: const Color(0xffA7A7A7)),
                      );
                    } else {
                      ///无更多数据
                      footer = Text(
                        '没有更多数据啦'.tr,
                        style: TextStyle(fontSize: 14.sp, color: const Color(0xffA7A7A7)),
                      );
                    }
                    return SizedBox(
                      height: 20.w,
                      child: Center(child: footer),
                    );
                  },
                ),
                child: widget.child
              ),
            )
          )),

          /// 未加载前显示的动画，加载之后需要隐藏
          Obx(() => Visibility(
            visible: widget.getController.loadState.value == 0,
            // child: const Center(child: Text("加载中"),)
            child: const Center(
              child: CustomLoading(),
            ),
          )),

          /// 加载数据为空的页面
          Obx(() => Visibility(
            visible: widget.getController.loadState.value == 2,
            // child: const Center(child: Text("暂无数据"),)
            child: widget.empty ?? const Center(
              child: CustomEmpty(),
            ),
          )),

          /// 加载出错的页面
          Obx(() => Visibility(
            visible: widget.getController.loadState.value == 3,
            // child: const NoData(),
            child: const Center(
              child: CustomError(),
            )
          )),
        ],
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;

}